package com.example.fw;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;


import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

	public class ContactHelper extends HelperWithWebDriverBase{
	
	private Set<ContactObject> contactCache;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public Set<ContactObject> getContacts() {
	
		if (contactCache == null) {
			buildContactCache();
		}
		return contactCache;
	}
	
	private void buildContactCache() {
		contactCache = new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
		WebElement Contactform = findElements(By.tagName("form")).get(2);
		List<WebElement> checkboxes = Contactform.findElements(By.name("selected[]"));
		
		
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			Pattern p = Pattern.compile(".*\\((.*)\\)");
			Matcher m = p.matcher(title);
			if (m.matches())
			{
				title = m.group(1);
			}
			
			ContactObject contact = new ContactObject()
			.setFirstname(title)
			.setId(checkbox.getAttribute("value"));
			contactCache.add(contact);
		}
		
	}

	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		initContactCreation();
		fillContactForm(contactObject);
		submitContactCreation();
		manager.getNavigationHelper().goToHomePage();
		contactCache = null;
	}

	public void modifyContact(int i, ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		contactObject.setId(initContactModification(i));
		fillContactModificationForm(contactObject);
		submitContactModification();
		manager.getNavigationHelper().goToHomePage();
		contactCache = null;
		manager.getHibernateHelper().clearSession();
	}
	
	public String deleteContact(int i) {
		manager.getNavigationHelper().openMainPage();
		String deletedContactId = initContactModification(i);
		initContactRemoval();
		manager.getNavigationHelper().goToHomePage();
		contactCache = null;
		return deletedContactId;
	}

	private void initContactRemoval() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}
	
	private void initContactCreation() {
		click(By.linkText("add new"));
	}

	private void fillGenaralContactFields(ContactObject contactObject) {
		type("firstname", contactObject.firstname);
		type("lastname", contactObject.lastname);
		type("address", contactObject.address);
		type("home", contactObject.hometelephone);
		type("mobile", contactObject.mobilelephone);
		type("work", contactObject.worktelephone);
		type("email", contactObject.email);
		type("email2", contactObject.email2);
		if(contactObject.birthday != null && contactObject.birthday != "")
			new Select(findElement(By.name("bday"))).selectByVisibleText(contactObject.birthday);
		if(contactObject.birthmonth != null && contactObject.birthmonth != "")
			new Select(findElement(By.name("bmonth"))).selectByVisibleText(contactObject.birthmonth);
		if(contactObject.birthyear != null)
			type("byear", contactObject.birthyear);
		if(contactObject.address2 != null)
			type("address2", contactObject.address2);
		if(contactObject.phone2 != null)
			type("phone2", contactObject.phone2);
		if(contactObject.group != null)
			new Select(findElement(By.name("new_group"))).selectByVisibleText(contactObject.group);
	
	}

	private void fillContactForm(ContactObject contactObject) {
		fillGenaralContactFields(contactObject);
	}
	
	private void submitContactCreation() {
		click(By.name("submit"));
	}

	private void submitContactModification() {
		click(By.name("update"));
	}

	private String initContactModification(int i) {
		//click(By.cssSelector("img[alt=\"Edit\"]"));
		
		String returnId = "";
		
		WebElement Contactform = findElements(By.tagName("form")).get(2);
		List<WebElement> checkboxes = Contactform.findElements(By.name("selected[]"));
		int counter = 0;
		for (WebElement checkbox : checkboxes) {
			if (counter == i) {
				returnId = checkbox.getAttribute("value");
				click(By.cssSelector("img[alt=\"Edit\"]"));
				break;
			}
			counter ++;
		}
		
	
		return returnId;
	}
	
	private void fillContactModificationForm(ContactObject contactObject) {
		fillGenaralContactFields(contactObject);
	}





}
